/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.zx.examination.barbecue.web;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.collect.MapUtils;
import com.jeesite.common.idgen.IdGen;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.modules.sys.entity.Post;
import com.jeesite.modules.sys.service.PostService;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.zx.audio.bank.entity.TaudioBank;
import com.jeesite.modules.zx.audio.bank.service.TaudioBankService;
import com.jeesite.modules.zx.course.entity.Tcourses;
import com.jeesite.modules.zx.course.service.TcoursesService;
import com.jeesite.modules.zx.examination.barbecue.entity.TexaminationBarbecue;
import com.jeesite.modules.zx.examination.barbecue.post.entity.TexaminationBarbecuePost;
import com.jeesite.modules.zx.examination.barbecue.post.service.TexaminationBarbecuePostService;
import com.jeesite.modules.zx.examination.barbecue.service.TexaminationBarbecueService;
import com.jeesite.modules.zx.examination.entity.Texamination;
import com.jeesite.modules.zx.examination.service.TexaminationService;
import com.jeesite.modules.zx.picture.bank.entity.TpictureBank;
import com.jeesite.modules.zx.picture.bank.service.TpictureBankService;
import com.jeesite.modules.zx.problem.bank.entity.TproblemBank;
import com.jeesite.modules.zx.problem.bank.service.TproblemBankService;
import com.jeesite.modules.zx.question.bank.entity.TquestionBank;
import com.jeesite.modules.zx.question.bank.service.TquestionBankService;
import com.jeesite.modules.zx.reading.bank.entity.TreadingBank;
import com.jeesite.modules.zx.reading.bank.service.TreadingBankService;
import com.jeesite.modules.zx.subject.bank.entity.TsubjectBank;
import com.jeesite.modules.zx.subject.bank.service.TsubjectBankService;

/**
 * 理论考试表Controller
 * @author zpp
 * @version 2020-01-19
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/examination/barbecue/texaminationBarbecue")
public class TexaminationBarbecueController extends BaseController {

	@Autowired
	private TexaminationBarbecueService texaminationBarbecueService;
	@Autowired
	private TcoursesService tcoursesService;
	@Autowired
	private PostService postService;
	@Autowired
	private TquestionBankService tquestionBankService;
	@Autowired
	private TexaminationService texaminationService;
	@Autowired
	private TexaminationBarbecuePostService texaminationBarbecuePostService;
	@Autowired
	private TreadingBankService treadingBankService;
	@Autowired
	private TpictureBankService tpictureBankService;
	@Autowired
	private TaudioBankService taudioBankService;
	@Autowired
	private TsubjectBankService tsubjectBankService;
	@Autowired
	private TproblemBankService tproblemBankService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TexaminationBarbecue get(String id, boolean isNewRecord) {
		return texaminationBarbecueService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("zx:examination:barbecue:texaminationBarbecue:view")
	@RequestMapping(value = {"list", ""})
	public String list(TexaminationBarbecue texaminationBarbecue, Model model) {
		model.addAttribute("texaminationBarbecue", texaminationBarbecue);
		return "modules/zx/examination/barbecue/texaminationBarbecueList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("zx:examination:barbecue:texaminationBarbecue:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public List<TexaminationBarbecue> listData(TexaminationBarbecue texaminationBarbecue) {
		if (StringUtils.isBlank(texaminationBarbecue.getParentCode())) {
			texaminationBarbecue.setParentCode(TexaminationBarbecue.ROOT_CODE);
		}
		if (StringUtils.isNotBlank(texaminationBarbecue.getObjName())){
			texaminationBarbecue.setParentCode(null);
		}
		if (StringUtils.isNotBlank(texaminationBarbecue.getObjSimple())){
			texaminationBarbecue.setParentCode(null);
		}
		if (texaminationBarbecue.getObjType() != null){
			texaminationBarbecue.setParentCode(null);
		}
		if (texaminationBarbecue.getBarbecueStartDate() != null){
			texaminationBarbecue.setParentCode(null);
		}
		if (texaminationBarbecue.getBarbecueEndDate() != null){
			texaminationBarbecue.setParentCode(null);
		}
		if (StringUtils.isNotBlank(texaminationBarbecue.getRemarks())){
			texaminationBarbecue.setParentCode(null);
		}
		if (texaminationBarbecue.getObjExplain() != null){
			texaminationBarbecue.setParentCode(null);
		}
		if (StringUtils.isNotBlank(texaminationBarbecue.getObjAnswer())){
			texaminationBarbecue.setParentCode(null);
		}
		List<TexaminationBarbecue> list = texaminationBarbecueService.findList(texaminationBarbecue);
		for(TexaminationBarbecue exam: list){
			String parentCode = exam.getParentCode();
			if(StringUtils.isNotBlank(parentCode)&&parentCode.equals("0")){
				String id=exam.getId();
				TexaminationBarbecue childExam=new TexaminationBarbecue();
				childExam.setParentCode(id);
				List<TexaminationBarbecue> findList = texaminationBarbecueService.findList(childExam);
				BigDecimal parentMaxScore=new BigDecimal(0);
				BigDecimal parentJishu=new BigDecimal(0);
				for(TexaminationBarbecue exbarbe: findList){
					BigDecimal maxScore = exbarbe.getMaxScore();
					if(maxScore==null) maxScore=new BigDecimal(0);
					parentMaxScore=parentMaxScore.add(maxScore);
					BigDecimal jishu = exbarbe.getQuestionScore();
					if(jishu==null) jishu=new BigDecimal(0);
					parentJishu=parentJishu.add(jishu);
				}
				exam.setMaxScore(parentMaxScore);
				exam.setQuestionScore(parentJishu);
				texaminationBarbecueService.save(exam);
			}
		}
		return list;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("zx:examination:barbecue:texaminationBarbecue:view")
	@RequestMapping(value = "form")
	public String form(TexaminationBarbecue texaminationBarbecue, Model model,HttpServletRequest request,HttpServletResponse response) {
		// 创建并初始化下一个节点信息
		texaminationBarbecue = createNextNode(texaminationBarbecue);
		Post post=new Post();
		List<Post> postList=postService.findList(post);
		model.addAttribute("postList", postList);
		model.addAttribute("texaminationBarbecue", texaminationBarbecue);
		String objExplain=request.getParameter("objExplain");
		if(StringUtils.isNotBlank(objExplain)){
			if(objExplain.equals("0")){
				Tcourses tcourses=new Tcourses();
				tcourses.setParentCode("0");
				List<Tcourses> findList = tcoursesService.findList(tcourses);
				model.addAttribute("courseList", findList);
			}
			if(objExplain.equals("3")){
				TquestionBank quetionBank=new TquestionBank();
				quetionBank.setObjType(texaminationBarbecue.getObjType());
				//查询单选
				List<TquestionBank> findList = tquestionBankService.findList(quetionBank);
				model.addAttribute("questionList",findList);
			}
			return "modules/zx/examination/barbecue/texaminationBarbecueForm"+objExplain;
		}
		return "modules/zx/examination/barbecue/texaminationBarbecueForm";
	}
	@RequiresPermissions("zx:examination:barbecue:texaminationBarbecue:view")
	@RequestMapping(value = "form0")
	public String form0(TexaminationBarbecue texaminationBarbecue, Model model,HttpServletRequest request,HttpServletResponse response) {
		// 创建并初始化下一个节点信息
		texaminationBarbecue = createNextNode(texaminationBarbecue);
		model.addAttribute("texaminationBarbecue", texaminationBarbecue);
		String objExplain=request.getParameter("objExplain");
		if(StringUtils.isNotBlank(objExplain)){
			Tcourses tcourses=new Tcourses();
			tcourses.setParentCode("0");
			List<Tcourses> findList = tcoursesService.findList(tcourses);
			model.addAttribute("courseList", findList);
			return "modules/zx/examination/barbecue/texaminationBarbecueForm"+objExplain;
		}
		return "modules/zx/examination/barbecue/texaminationBarbecueForm";
	}
	@RequiresPermissions("zx:examination:barbecue:texaminationBarbecue:view")
	@RequestMapping(value = "form1")
	public String form1(TexaminationBarbecue texaminationBarbecue, Model model,HttpServletRequest request,HttpServletResponse response) {
		// 创建并初始化下一个节点信息
		texaminationBarbecue = createNextNode(texaminationBarbecue);
		Post post=new Post();
		List<Post> postList=postService.findList(post);
		model.addAttribute("postList", postList);
		model.addAttribute("texaminationBarbecue", texaminationBarbecue);
		String objExplain=request.getParameter("objExplain");
		if(StringUtils.isNotBlank(objExplain)){
			return "modules/zx/examination/barbecue/texaminationBarbecueForm"+objExplain;
		}
		return "modules/zx/examination/barbecue/texaminationBarbecueForm";
	}
	@RequiresPermissions("zx:examination:barbecue:texaminationBarbecue:view")
	@RequestMapping(value = "form2")
	public String form2(TexaminationBarbecue texaminationBarbecue, Model model,HttpServletRequest request,HttpServletResponse response) {
		// 创建并初始化下一个节点信息
		texaminationBarbecue = createNextNode(texaminationBarbecue);
		model.addAttribute("texaminationBarbecue", texaminationBarbecue);
		String objExplain=request.getParameter("objExplain");
		if(StringUtils.isNotBlank(objExplain)){
			return "modules/zx/examination/barbecue/texaminationBarbecueForm"+objExplain;
		}
		return "modules/zx/examination/barbecue/texaminationBarbecueForm";
	}
	@RequiresPermissions("zx:examination:barbecue:texaminationBarbecue:view")
	@RequestMapping(value = "form3")
	public String form3(TexaminationBarbecue texaminationBarbecue, Model model,HttpServletRequest request,HttpServletResponse response) {
		// 创建并初始化下一个节点信息
		texaminationBarbecue = createNextNode(texaminationBarbecue);
		//1.获取考试类别 0,19,19002,
		Integer category=null;
		String examId = texaminationBarbecue.getExamId();
		
		Texamination texamination = texaminationService.get(examId);
		category=texamination.getObjType();
		List<String> postList=new ArrayList<>();
		String parentCodes=texaminationBarbecue.getParent().getParentCodes();
		String[] split = parentCodes.split(",");
		if(split!=null&&split.length>0){
//			String firstId=split[1];
//			if(StringUtils.isNotBlank(firstId)){
//			  TexaminationBarbecue firstEntity = texaminationBarbecueService.get(firstId);
//			  category = firstEntity.getCategory();
//			}
			String secondId=split[1];
			if(StringUtils.isNotBlank(secondId)){
				  TexaminationBarbecue secondEntity = texaminationBarbecueService.get(secondId);
				  if(secondEntity!=null){
					  String post = secondEntity.getPost();
					  if(StringUtils.isNotBlank(post)){
						  String[] split2 = post.split(",");
						  for(String postCode:split2){
							  if(StringUtils.isNotBlank(postCode)){
							    postList.add(postCode);
							  }
						  }
					  }
				  }
			}
		}
		Integer objType = texaminationBarbecue.getObjType();
		TquestionBank question=new TquestionBank();
		question.setObjType(objType);
		if(category!=null){
			question.setCategory(category);
		}
        if(postList.size()>0){
        	question.getSqlMap().getWhere().and("post", QueryType.IN, postList);
		}else{
			question.getSqlMap().getWhere().and("post",QueryType.IS_NULL, null);
		}
        List<TquestionBank> questionList = tquestionBankService.findList(question);
		model.addAttribute("questionList", questionList);
		model.addAttribute("texaminationBarbecue", texaminationBarbecue);
		String objExplain=request.getParameter("objExplain");
		if(StringUtils.isNotBlank(objExplain)){
			return "modules/zx/examination/barbecue/texaminationBarbecueForm"+objExplain;
		}
		return "modules/zx/examination/barbecue/texaminationBarbecueForm";
	}
	/**
	 * 创建并初始化下一个节点信息，如：排序号、默认值
	 */
	@RequiresPermissions("zx:examination:barbecue:texaminationBarbecue:edit")
	@RequestMapping(value = "createNextNode")
	@ResponseBody
	public TexaminationBarbecue createNextNode(TexaminationBarbecue texaminationBarbecue) {
		if (StringUtils.isNotBlank(texaminationBarbecue.getParentCode())){
			texaminationBarbecue.setParent(texaminationBarbecueService.get(texaminationBarbecue.getParentCode()));
		}
		if (texaminationBarbecue.getIsNewRecord()) {
			TexaminationBarbecue where = new TexaminationBarbecue();
			where.setParentCode(texaminationBarbecue.getParentCode());
			TexaminationBarbecue last = texaminationBarbecueService.getLastByParentCode(where);
			// 获取到下级最后一个节点
			if (last != null){
				texaminationBarbecue.setTreeSort(last.getTreeSort() + 30);
				texaminationBarbecue.setId(IdGen.nextCode(last.getId()));
			}else if (texaminationBarbecue.getParent() != null){
				texaminationBarbecue.setId(texaminationBarbecue.getParent().getId() + "001");
			}
		}
		// 以下设置表单默认数据
		if (texaminationBarbecue.getTreeSort() == null){
			texaminationBarbecue.setTreeSort(TexaminationBarbecue.DEFAULT_TREE_SORT);
		}
		return texaminationBarbecue;
	}

	/**
	 * 保存理论考试表
	 */
	@RequiresPermissions("zx:examination:barbecue:texaminationBarbecue:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TexaminationBarbecue texaminationBarbecue) {
		Integer objType=texaminationBarbecue.getObjType();
		
		texaminationBarbecueService.save(texaminationBarbecue);
		String examId = texaminationBarbecue.getExamId();
		Integer objExplain = texaminationBarbecue.getObjExplain();
		if(objExplain!=null&&objExplain==1){
			String post = texaminationBarbecue.getPost();
			String id=texaminationBarbecue.getId();
			String[] split = post.split(",");
			TexaminationBarbecuePost entityOld=new TexaminationBarbecuePost();
			entityOld.setParentId(examId);
			entityOld.setBarbecueId(id);
			List<TexaminationBarbecuePost> oldList = texaminationBarbecuePostService.findList(entityOld);
			List<String> olist=new ArrayList<String>();
			for(TexaminationBarbecuePost old: oldList){
				String post2 = old.getPost();
				olist.add(post2);
			}
			List<String> newList=new ArrayList<String>();
			for(String key:split){
				newList.add(key);
				if(!olist.contains(key)){
					TexaminationBarbecuePost entity=new TexaminationBarbecuePost();
					entity.setPost(key);
					entity.setParentId(examId);
					entity.setBarbecueId(id);
					texaminationBarbecuePostService.save(entity);
				}
			}
			for(TexaminationBarbecuePost oldEnt: oldList){
				String post2 = oldEnt.getPost();
				if(!newList.contains(post2)){
					texaminationBarbecuePostService.delete(oldEnt);
				}
			}
		}
		return renderResult(Global.TRUE, text("保存理论考试表成功！"));
	}
	@RequiresPermissions("zx:examination:barbecue:texaminationBarbecue:edit")
	@PostMapping(value = "save2")
	@ResponseBody
	public String save2(@Validated TexaminationBarbecue texaminationBarbecue) {
		Integer objType=texaminationBarbecue.getObjType();		
		texaminationBarbecue.setTreeLeaf("0");
		texaminationBarbecueService.save(texaminationBarbecue);
		BigDecimal maxScore = texaminationBarbecue.getMaxScore();
		BigDecimal jishu = texaminationBarbecue.getQuestionScore();
		if(jishu==null){
			jishu=new BigDecimal(0); 
		}
		if(jishu.compareTo(maxScore)==0){
			return renderResult(Global.TRUE, text("保存理论考试表成功！"));
		}
		
		if(objType!=null){
			switch (objType) {
			case 0://单选
				multiSaveQuestion(texaminationBarbecue);
				break;
			case 1://多选
				multiSaveQuestion(texaminationBarbecue);
				break;
			case 2://判断
				multiSaveQuestion(texaminationBarbecue);
				break;
			case 3://阅读理解
				readingSaveQuestion(texaminationBarbecue);
				break;
			case 4://图片题
				tpictureSaveQuestion(texaminationBarbecue);
				break;
			case 5://音频题
				audioSaveQuestion(texaminationBarbecue);
				break;
			case 6://案例题
				multiSaveQuestion(texaminationBarbecue);
				break;
			default:
				break;
			}
			
		}
		return renderResult(Global.TRUE, text("保存理论考试表成功！"));
	}
	
	private void audioSaveQuestion(TexaminationBarbecue texaminationBarbecue) {
		TaudioBank audio=new TaudioBank();
		audio.getSqlMap().getOrder().setOrderBy("RAND() LIMIT 1");
		List<TaudioBank> findList = taudioBankService.findList(audio);
		BigDecimal maxScore = texaminationBarbecue.getMaxScore();
		BigDecimal jishu = texaminationBarbecue.getQuestionScore();
		if(jishu==null){
			jishu=new BigDecimal(0); 
		}
		if(findList!=null&&findList.size()>0){
			TaudioBank taudioBank = findList.get(0);
			String id = taudioBank.getId();
			TsubjectBank subject=new TsubjectBank();
			subject.setParentId(id);
			List<TsubjectBank> subjectList = tsubjectBankService.findList(subject);
			for(TsubjectBank subj:subjectList){
				TproblemBank problem=new TproblemBank();
				problem.setSubjectId(subj.getId());
				List<TproblemBank> problemList = tproblemBankService.findList(problem);
				for(TproblemBank prob:problemList){
					Double score = prob.getScore();
					if(score==null) score=0.0;
					jishu=jishu.add(new BigDecimal(score));
					if(jishu.compareTo(maxScore)==1){
						jishu=jishu.subtract(new BigDecimal(score));
						break;
					}
				}
			}
			TexaminationBarbecue question=new TexaminationBarbecue();
			question.setParentCode(texaminationBarbecue.getId());
			 question = createNextNode(question);
			question.setObjName(id);
			question.setExamId(texaminationBarbecue.getExamId());
			question.setObjType(texaminationBarbecue.getObjType());
			question.setObjExplain(3);
			question.setMaxScore(jishu);
			question.setQuestionScore(jishu);
			texaminationBarbecueService.save(question);
		}
		boolean isNewRecord = texaminationBarbecue.getIsNewRecord();
		 if(isNewRecord) texaminationBarbecue.setIsNewRecord(false);
		 texaminationBarbecue.setQuestionScore(jishu);
		 texaminationBarbecueService.save(texaminationBarbecue);
	}

	private void tpictureSaveQuestion(TexaminationBarbecue texaminationBarbecue) {
		TpictureBank picture=new TpictureBank();
		picture.getSqlMap().getOrder().setOrderBy("RAND() LIMIT 1");
		List<TpictureBank> findList = tpictureBankService.findList(picture);
		BigDecimal maxScore = texaminationBarbecue.getMaxScore();
		BigDecimal jishu = texaminationBarbecue.getQuestionScore();
		if(jishu==null){
			jishu=new BigDecimal(0); 
		}
		if(findList!=null&&findList.size()>0){
			TpictureBank tpictureBank = findList.get(0);
			String id = tpictureBank.getId();
			TsubjectBank subject=new TsubjectBank();
			subject.setParentId(id);
			List<TsubjectBank> subjectList = tsubjectBankService.findList(subject);
			for(TsubjectBank subj:subjectList){
				TproblemBank problem=new TproblemBank();
				problem.setSubjectId(subj.getId());
				List<TproblemBank> problemList = tproblemBankService.findList(problem);
				for(TproblemBank prob:problemList){
					Double score = prob.getScore();
					if(score==null) score=0.0;
					jishu=jishu.add(new BigDecimal(score));
					if(jishu.compareTo(maxScore)==1){
						jishu=jishu.subtract(new BigDecimal(score));
						break;
					}
				}
			}
			TexaminationBarbecue question=new TexaminationBarbecue();
			question.setParentCode(texaminationBarbecue.getId());
			 question = createNextNode(question);
			question.setObjName(id);
			question.setExamId(texaminationBarbecue.getExamId());
			question.setObjType(texaminationBarbecue.getObjType());
			question.setObjExplain(3);
			question.setMaxScore(jishu);
			question.setQuestionScore(jishu);
			texaminationBarbecueService.save(question);
		}
		 boolean isNewRecord = texaminationBarbecue.getIsNewRecord();
		 if(isNewRecord) texaminationBarbecue.setIsNewRecord(false);
		 texaminationBarbecue.setQuestionScore(jishu);
		 texaminationBarbecueService.save(texaminationBarbecue);
	}

	private void readingSaveQuestion(TexaminationBarbecue texaminationBarbecue) {
		TreadingBank readingBank=new TreadingBank();
		readingBank.getSqlMap().getOrder().setOrderBy("RAND() LIMIT 1");
		List<TreadingBank> findList = treadingBankService.findList(readingBank);
		BigDecimal maxScore = texaminationBarbecue.getMaxScore();
		BigDecimal jishu = texaminationBarbecue.getQuestionScore();
		if(jishu==null){
			jishu=new BigDecimal(0); 
		}
		if(findList!=null&&findList.size()>0){
			TreadingBank treadingBank = findList.get(0);
			String id = treadingBank.getId();
			TsubjectBank subject=new TsubjectBank();
			subject.setParentId(id);
			List<TsubjectBank> subjectList = tsubjectBankService.findList(subject);
			for(TsubjectBank subj:subjectList){
				TproblemBank problem=new TproblemBank();
				problem.setSubjectId(subj.getId());
				List<TproblemBank> problemList = tproblemBankService.findList(problem);
				for(TproblemBank prob:problemList){
					Double score = prob.getScore();
					if(score==null) score=0.0;
					jishu=jishu.add(new BigDecimal(score));
					if(jishu.compareTo(maxScore)==1){
						jishu=jishu.subtract(new BigDecimal(score));
						break;
					}
				}
			}
			TexaminationBarbecue question=new TexaminationBarbecue();
			question.setParentCode(texaminationBarbecue.getId());
			 question = createNextNode(question);
			question.setObjName(id);
			question.setExamId(texaminationBarbecue.getExamId());
			question.setObjType(texaminationBarbecue.getObjType());
			question.setObjExplain(3);
			question.setMaxScore(jishu);
			question.setQuestionScore(jishu);
			texaminationBarbecueService.save(question);
		}
		 boolean isNewRecord = texaminationBarbecue.getIsNewRecord();
		 if(isNewRecord) texaminationBarbecue.setIsNewRecord(false);
		 texaminationBarbecue.setQuestionScore(jishu);
		 texaminationBarbecueService.save(texaminationBarbecue);
	}

	private void multiSaveQuestion(TexaminationBarbecue texaminationBarbecue) {
		 int num=30;
		 TquestionBank questionBank=new TquestionBank();
		 String post = texaminationBarbecue.getPost();
		 Integer category = texaminationBarbecue.getCategory();
		 if(category!=null){
			 questionBank.setCategory(category);
		 }
		 if(StringUtils.isNotBlank(post)){
			 String[] split = post.split(",");
			 questionBank.getSqlMap().getWhere().and("post", QueryType.IN, split);
		 }
	     questionBank.setObjType(texaminationBarbecue.getObjType());
	     questionBank.getSqlMap().getOrder().setOrderBy("RAND() LIMIT "+num);
		 List<TquestionBank> findList = tquestionBankService.findList(questionBank);
		 BigDecimal maxScore = texaminationBarbecue.getMaxScore();
		 BigDecimal jishu = texaminationBarbecue.getQuestionScore();
		 if(jishu==null){
		    jishu=new BigDecimal(0); 
		 }
		 for(TquestionBank tb:findList){
			Double score = tb.getScore();
			if(score==null) score=0.0;
			jishu=jishu.add(new BigDecimal(score));
			if(jishu.compareTo(maxScore)==1){
				jishu=jishu.subtract(new BigDecimal(score));
				break;
			}else{
				TexaminationBarbecue question=new TexaminationBarbecue();
				question.setParentCode(texaminationBarbecue.getId());
				 question = createNextNode(question);
				question.setObjName(tb.getId());
				question.setExamId(texaminationBarbecue.getExamId());
				question.setObjType(texaminationBarbecue.getObjType());
				question.setObjExplain(3);
				question.setMaxScore(new BigDecimal(tb.getScore()));
				question.setQuestionScore(new BigDecimal(tb.getScore()));
				question.setCategory(category);
				texaminationBarbecueService.save(question);
			}
		 }
		 boolean isNewRecord = texaminationBarbecue.getIsNewRecord();
		 if(isNewRecord) texaminationBarbecue.setIsNewRecord(false);
		 texaminationBarbecue.setQuestionScore(jishu);
		 texaminationBarbecue.setTreeLeaf("0");
		 texaminationBarbecueService.save(texaminationBarbecue);
	}

	@RequiresPermissions("zx:examination:barbecue:texaminationBarbecue:edit")
	@PostMapping(value = "save3")
	@ResponseBody
	public String save3(@Validated TexaminationBarbecue texaminationBarbecue) {
			String parentCode = texaminationBarbecue.getParentCode();
			TexaminationBarbecue parentBarbecue = texaminationBarbecueService.get(parentCode);
			String objName = texaminationBarbecue.getObjName();
			if(StringUtils.isNotBlank(objName)){
				TquestionBank tquestionBank = tquestionBankService.get(objName);
				Double score = tquestionBank.getScore();
				texaminationBarbecue.setQuestionScore(new BigDecimal(score));
				texaminationBarbecue.setMaxScore(new BigDecimal(score));
				BigDecimal questionScore = parentBarbecue.getQuestionScore();
				if(questionScore==null){
					questionScore=new BigDecimal(0);
				}
				BigDecimal maxScore = parentBarbecue.getMaxScore();
				if(maxScore==null){
					maxScore=new BigDecimal(0);
				}
				questionScore=questionScore.add(new BigDecimal(score));
				if(questionScore.compareTo(maxScore)==1){
					return renderResult(Global.FALSE, text("以达到最大分数"));
				}
				parentBarbecue.setQuestionScore(questionScore);
				
				String pparentCode = parentBarbecue.getParentCode();
				TexaminationBarbecue pparentBarbecue = texaminationBarbecueService.get(pparentCode);
				BigDecimal questionScore2 = pparentBarbecue.getQuestionScore();
				if(questionScore2==null){
					questionScore2=new BigDecimal(0);
				}
				questionScore2=questionScore2.add(new BigDecimal(score));
				BigDecimal maxScore2 = pparentBarbecue.getMaxScore();
				if(maxScore2==null){
					maxScore2=new BigDecimal(0);
				}
				if(questionScore2.compareTo(maxScore2)==1){
					return renderResult(Global.FALSE, text("以达到最大分数"));
				}
				pparentBarbecue.setQuestionScore(questionScore2);
//				Integer objType = pparentBarbecue.getObjType();
//				if(objType>2){
//					String ppparentCode = pparentBarbecue.getParentCode();
//					TexaminationBarbecue ppparentBarbecue = texaminationBarbecueService.get(ppparentCode);
//					BigDecimal questionScore3=ppparentBarbecue.getQuestionScore();
//					if(questionScore3==null){
//						questionScore3=new BigDecimal(0);
//					}
//					questionScore3=questionScore3.add(new BigDecimal(score));
//					BigDecimal maxScore3 = ppparentBarbecue.getMaxScore();
//					if(maxScore3==null){
//						maxScore3=new BigDecimal(0);
//					}
//					if(questionScore3.compareTo(maxScore3)==1){
//						return renderResult(Global.FALSE, text("以达到最大分数"));
//					}
//					ppparentBarbecue.setQuestionScore(questionScore3);
//					texaminationBarbecueService.update(ppparentBarbecue);
//				}
//				texaminationBarbecueService.update(parentBarbecue);
			}
		texaminationBarbecueService.save(texaminationBarbecue);
		return renderResult(Global.TRUE, text("保存理论考试表成功！"));
	}
	/**
	 * 停用理论考试表
	 */
	@RequiresPermissions("zx:examination:barbecue:texaminationBarbecue:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(TexaminationBarbecue texaminationBarbecue) {
		TexaminationBarbecue where = new TexaminationBarbecue();
		where.setStatus(TexaminationBarbecue.STATUS_NORMAL);
		where.setParentCodes("," + texaminationBarbecue.getId() + ",");
		long count = texaminationBarbecueService.findCount(where);
		if (count > 0) {
			return renderResult(Global.FALSE, text("该理论考试表包含未停用的子理论考试表！"));
		}
		texaminationBarbecue.setStatus(TexaminationBarbecue.STATUS_DISABLE);
		texaminationBarbecueService.updateStatus(texaminationBarbecue);
		return renderResult(Global.TRUE, text("停用理论考试表成功"));
	}
	
	/**
	 * 启用理论考试表
	 */
	@RequiresPermissions("zx:examination:barbecue:texaminationBarbecue:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(TexaminationBarbecue texaminationBarbecue) {
		texaminationBarbecue.setStatus(TexaminationBarbecue.STATUS_NORMAL);
		texaminationBarbecueService.updateStatus(texaminationBarbecue);
		return renderResult(Global.TRUE, text("启用理论考试表成功"));
	}
	
	/**
	 * 删除理论考试表
	 */
	@RequiresPermissions("zx:examination:barbecue:texaminationBarbecue:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TexaminationBarbecue texaminationBarbecue) {
		texaminationBarbecueService.delete(texaminationBarbecue);
		return renderResult(Global.TRUE, text("删除理论考试表成功！"));
	}
	
	/**
	 * 获取树结构数据
	 * @param excludeCode 排除的Code
	 * @param isShowCode 是否显示编码（true or 1：显示在左侧；2：显示在右侧；false or null：不显示）
	 * @return
	 */
	@RequiresPermissions("zx:examination:barbecue:texaminationBarbecue:view")
	@RequestMapping(value = "treeData")
	@ResponseBody
	public List<Map<String, Object>> treeData(String excludeCode, String isShowCode) {
		List<Map<String, Object>> mapList = ListUtils.newArrayList();
		List<TexaminationBarbecue> list = texaminationBarbecueService.findList(new TexaminationBarbecue());
		for (int i=0; i<list.size(); i++){
			TexaminationBarbecue e = list.get(i);
			// 过滤非正常的数据
			if (!TexaminationBarbecue.STATUS_NORMAL.equals(e.getStatus())){
				continue;
			}
			// 过滤被排除的编码（包括所有子级）
			if (StringUtils.isNotBlank(excludeCode)){
				if (e.getId().equals(excludeCode)){
					continue;
				}
				if (e.getParentCodes().contains("," + excludeCode + ",")){
					continue;
				}
			}
			Map<String, Object> map = MapUtils.newHashMap();
			map.put("id", e.getId());
			map.put("pId", e.getParentCode());
			map.put("name", StringUtils.getTreeNodeName(isShowCode, e.getId(), e.getObjName()));
			mapList.add(map);
		}
		return mapList;
	}

	/**
	 * 修复表结构相关数据
	 */
	@RequiresPermissions("zx:examination:barbecue:texaminationBarbecue:edit")
	@RequestMapping(value = "fixTreeData")
	@ResponseBody
	public String fixTreeData(TexaminationBarbecue texaminationBarbecue){
		if (!UserUtils.getUser().isAdmin()){
			return renderResult(Global.FALSE, "操作失败，只有管理员才能进行修复！");
		}
		texaminationBarbecueService.fixTreeData();
		return renderResult(Global.TRUE, "数据修复成功");
	}
	
}